package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import top.deepdesigner.pojo.DtsGoods;
import top.deepdesigner.pojo.DtsGrouponRules;
import top.deepdesigner.service.DtsGoodsService;
import top.deepdesigner.service.DtsGrouponRulesService;

import java.util.Date;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/23 星期五 20:43:43 <br/>
 */
@Service
public class DtsGrouponRulesServiceImpl extends ServiceImpl<BaseMapper<DtsGrouponRules>, DtsGrouponRules> implements DtsGrouponRulesService {

  @Autowired
  private DtsGoodsService goodsService;

  @Override
  public Long getCouponCount() {
    return this.count(new LambdaQueryWrapper<DtsGrouponRules>().eq(true, DtsGrouponRules::getDeleted, 0));
  }

  @Override
  public List<DtsGrouponRules> getCouponList(Integer page, Integer limit, String sort, String order, Integer goodsId) {
    QueryWrapper<DtsGrouponRules> wrapper = new QueryWrapper<>();
    wrapper.eq(true, "deleted", 0)
        .eq(goodsId != null, "goods_id", goodsId)
        .orderBy(!StringUtils.isEmpty(sort), "asc".equalsIgnoreCase(order), sort);
    return this.page(new Page<>(page, limit), wrapper).getRecords();
  }

  @Transactional
  @Override
  public DtsGrouponRules createCoupon(DtsGrouponRules grouponRules) {
    // TODO 查出来商品信息
    DtsGoods goods = goodsService.getGoodsByGoodId(grouponRules.getGoodsId());
    if (goods == null) {
      return null;
    }
    grouponRules.setAddTime(new Date());
    grouponRules.setGoodsName(goods.getName());
    grouponRules.setPicUrl(goods.getPicUrl());
    this.save(grouponRules);
    return grouponRules;
  }

  @Transactional
  @Override
  public DtsGrouponRules updateCoupon(DtsGrouponRules dtsGrouponRules) {
    LambdaUpdateWrapper<DtsGrouponRules> wrapper = new LambdaUpdateWrapper<>();
    wrapper.eq(true, DtsGrouponRules::getId, dtsGrouponRules.getId());
    this.update(dtsGrouponRules, wrapper);
    return dtsGrouponRules;
  }

  @Transactional
  @Override
  public void deleteCoupon(DtsGrouponRules dtsGrouponRules) {
    dtsGrouponRules.setUpdateTime(new Date());
    dtsGrouponRules.setDeleted(true);
    LambdaUpdateWrapper<DtsGrouponRules> wrapper = new LambdaUpdateWrapper<>();
    wrapper.eq(true, DtsGrouponRules::getId, dtsGrouponRules.getId());
    this.update(dtsGrouponRules, wrapper);
  }
}
